package org.jet.emall.prod.transfer;

import org.jet.emall.prod.dto.CategoryDTO;
import org.jet.emall.prod.dto.ProdAttrDTO;
import org.jet.emall.prod.dto.ProdSpecDTO;
import org.jet.emall.prod.vo.CategoryVO;
import org.jet.emall.prod.vo.ProdAttrVO;
import org.jet.emall.prod.vo.ProdSpecVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

import java.util.LinkedList;
import java.util.List;

/**
 * @author xiaozai
 * @version 1.0
 * @date 2020-04-15 16:29
 */
@Mapper
public interface CategoryVoTransfer {

    CategoryVoTransfer INSTANCE = Mappers.getMapper(CategoryVoTransfer.class);


    /**
     *  将商品分类DTO转VO
     * @param dto
     * @return
     */
    @Mappings({
            @Mapping(target = "children",ignore = true),
            @Mapping(target = "isLeaf",expression = "java(dto.getIsFloor()==1?false:true)")
    })

    CategoryVO toVo(CategoryDTO dto);


    /**
     *    将商品分类DTO列表转VO列表
     * @param categoryDTOList
     * @return
     */
    default List<CategoryVO> toVoList(List<CategoryDTO> categoryDTOList){
        List<CategoryVO> list = new LinkedList<>();
        categoryDTOList.forEach(dto->{
            CategoryVO vo = toVo(dto);
            if(dto.getChildren()!=null){
                List<CategoryVO> children = toVoList(dto.getChildren());
                vo.setChildren(children);
            }
            list.add(vo);
        });
        return list;
    }

    /**
     *  将属性DTO列表转VO列表
     * @param attrDTOList
     * @return
     */
    List<ProdAttrVO> toAttrVoList(List<ProdAttrDTO> attrDTOList);

    /**
     *  将规格DTO列表转VO列表
     * @param specDTOList
     * @return
     */
    List<ProdSpecVO> toSpecVoList(List<ProdSpecDTO> specDTOList);
}
